﻿using furion.blog.Domain.Shared;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Domain.Query.Message
{
    public class MessageListQuery : BaseQuery
    {

        public long? FromUser { get; set; } = null;
        public long? ToUser { get; set; } = null;
        public string IsDelete { get; set; } = string.Empty;
        public string IsOpen { get; set; } = string.Empty;
        public string SearchContent { get; set; } = string.Empty;
        /// <summary>
        /// 接收日期
        /// </summary>
        public string Date { get; set; } = string.Empty;

        private string Columns = " m.nick_name as `ToUserNickName`, m.avatar as `ToUserAvatar` , m2.nick_name as `FromUserNickName` , m2.avatar as `FromUserAvatar` " +
            ",t.message_id as `MessageId`,t.is_open as `IsOpen`,t.is_delete as `IsDelete`,t.ts as `Ts` " +
            ",t.created_time as `CreatedTime` ,t.content as `Content`," +
            "t.from_user as `FromUser`,t.to_user as `ToUser` " +
            "  ";

        private string Tables()
        {
            return " message t " +
                " left join member m on m.member_id=t.to_user " +
                " left join member m2 on m2.member_id=t.from_user ";
        }

        public string GetCountSQL()
        {
            var (strWhere, sqlParams) = QueryParams();
            return " select count(t.MessageId) from (select " + Columns + " from " + Tables() + " where 1=1 " + strWhere + " ) t ";
        }

        public string GetDataSQL()
        {
            var (strWhere, sqlParams) = QueryParams();
            return " select " + Columns + " from " + Tables() +
                " where t.ts<=(select t.ts from " + Tables() + " where 1=1 " + strWhere + " order by t.ts desc limit " + GetStartIndex() + ",1 ) " +
                " " + strWhere + " order by t.ts desc limit " + PageSize;
        }


        /// <summary>
        /// 返回查询where 和 条件参数
        /// </summary>
        /// <returns></returns>
        public (string, List<SugarParameter>) QueryParams()
        {
            List<SugarParameter> parameters = new List<SugarParameter>();
            StringBuilder strWhere = new StringBuilder(" ");

            if (!string.IsNullOrWhiteSpace(SearchContent)) {
                strWhere.AppendLine(" and ( m.nick_name like @searchContent OR m2.nick_name like @searchContent ) ");
                parameters.Add(new SugarParameter("searchContent", "%"+SearchContent+"%"));
            }
            if (FromUser!=null)
            {
                strWhere.AppendLine(" and t.from_user=@fromUser ");
                parameters.Add(new SugarParameter("fromUser", FromUser));
            }
            if (ToUser != null)
            {
                strWhere.AppendLine(" and t.to_user=@toUser ");
                parameters.Add(new SugarParameter("toUser", ToUser));
            }
            if (!string.IsNullOrWhiteSpace(IsDelete))
            {
                strWhere.AppendLine(" and t.is_delete=@isDelete ");
                parameters.Add(new SugarParameter("isDelete", IsDelete));
            }
            if (!string.IsNullOrWhiteSpace(IsOpen))
            {
                strWhere.AppendLine(" and t.is_open=@isOpen ");
                parameters.Add(new SugarParameter("isOpen", IsOpen));
            }
            if (!string.IsNullOrWhiteSpace(Date)) {
                strWhere.AppendLine(" and t.created_time between @startDate and @endDate ");
                parameters.Add(new SugarParameter("startDate", Date + " 00:00:00"));
                parameters.Add(new SugarParameter("endDate", Date + " 23:59:59"));
            }

            return (strWhere.ToString(), parameters);
        }
    }
}